//
//  Common+Ext.swift
//  TimMusic
//
//  Created by nkl on 2024/10/25.
//

import Foundation
import SwiftUI

extension UIFont {
    /// 返回该字体下，对应字符的宽度
    /// - Parameter word: 字符
    /// - Returns: 宽度
    func widthForWord(_ word: String) -> CGFloat {
        let sizeWidth = (word as NSString).size(withAttributes: [.font: self]).width
        return sizeWidth // .rounded(.up)
    }
}

extension Int {
    func localNumber(_ minimumIntegerDigits: Int? = nil, locale: Locale? = nil) -> String? {
        let formatter = NumberFormatter()
        if let locale = locale {
            formatter.locale = locale
        } else {
            formatter.locale = Locale.local
        }
//        formatter.locale = Locale(identifier: "ar")
        formatter.numberStyle = .decimal
        if let value = minimumIntegerDigits {
            formatter.minimumIntegerDigits = value
        }
        return formatter.string(for: self)
    }
}

extension Locale {
    static var local: Locale {
//        return Locale(identifier: "ar")
        return Locale(identifier: Locale.appLanguage)
    }

    static let appLanguage: String = {
        let systemLanguages = UserDefaults.standard.value(forKey: "AppleLanguages")
        var currentLanguage: String?
        if let arr = systemLanguages as? [String] {
            currentLanguage = arr.first
        } else if let str = systemLanguages as? String {
            currentLanguage = str
        }
        return currentLanguage ?? ""
    }()
}

extension UIColor {
    /// 必须要是RBGA模式的颜色
    func toColor() -> SwiftUI.Color {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0

        if let components = cgColor.components, components.count == 4 {
            r = components[0]
            g = components[1]
            b = components[2]
            a = components[3]
        } else {
            getRed(&r, green: &g, blue: &b, alpha: &a)
        }

        return SwiftUI.Color(red: r, green: g, blue: b, opacity: a)
    }
}

extension String {
    func toColor() -> UIColor? {
        if let rgba = toRgba() {
            return UIColor(red: rgba.r, green: rgba.g, blue: rgba.b, alpha: rgba.a)
        }

        return nil
    }

    func toRgba() -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat)? {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        var prefix: Int = 0

        let color: String = trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
        if color.hasPrefix("#") {
            prefix = 1
        } else if color.hasPrefix("0X") {
            prefix = 2
        }
        let length: Int = color.count - prefix
        //         RGB            RGBA          RRGGBB        RRGGBBAA

        func componentFrom(string: String, start: Int, length: Int) -> CGFloat {
            guard start >= 0, start + length <= string.count else {
                return 0
            }

            let begin = string.index(startIndex, offsetBy: start)
            let end = string.index(startIndex, offsetBy: start + length)

            let sub = string[begin ..< end]
            var full = String(sub)
            if length == 1 {
                full += full
            }

            var hex: UInt64 = 0
            Scanner(string: full).scanHexInt64(&hex)

            let float = CGFloat(hex) / 255.0
            return float
        }

        switch length {
        case 3: // RGB
            r = componentFrom(string: color, start: prefix, length: 1)
            g = componentFrom(string: color, start: prefix + 1, length: 1)
            b = componentFrom(string: color, start: prefix + 2, length: 1)
            a = 1
        case 4: // RGBA
            r = componentFrom(string: color, start: prefix, length: 1)
            g = componentFrom(string: color, start: prefix + 1, length: 1)
            b = componentFrom(string: color, start: prefix + 2, length: 1)
            a = componentFrom(string: color, start: prefix + 3, length: 1)
        case 6: // RRGGBB
            r = componentFrom(string: color, start: prefix, length: 2)
            g = componentFrom(string: color, start: prefix + 2, length: 2)
            b = componentFrom(string: color, start: prefix + 4, length: 2)
            a = 1
        case 8: // RRGGBBAA
            r = componentFrom(string: color, start: prefix, length: 2)
            g = componentFrom(string: color, start: prefix + 2, length: 2)
            b = componentFrom(string: color, start: prefix + 4, length: 2)
            a = componentFrom(string: color, start: prefix + 6, length: 2)
        default:
            return nil
        }

        return (r, g, b, a)
    }
}
